package com.guguskins.back.controller;


import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.guguskins.back.common.constant.PropConstant;
import com.guguskins.back.entity.GuProp;
import com.guguskins.back.entity.vo.ResultVo;
import com.guguskins.back.service.GuPropService;
import com.guguskins.back.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 道具表 前端控制器
 * </p>
 *
 * @author mybatis-plus
 * @since 2021-05-25
 */
@RestController
@RequestMapping("/prop")
public class GuPropController {

    @Autowired
    private GuPropService propService;

    @GetMapping("list")
//    @Authorized
    public ResultVo getList(@RequestHeader(value = "token", required = false, defaultValue = "") String token,
                            @RequestParam(value = "label", required = false, defaultValue = "0") int label) {
        Long userId = null;
        if (!ObjectUtils.isEmpty(token)) {
            userId = JwtUtil.verifyToken(token).get("userId", Long.class);
        }
        List<GuProp> props = propService.getListByUserId(userId, null, null, label == 0 ? null : label == 1 ? Arrays.asList(PropConstant.EXCLUDE_CARD, PropConstant.CUE_CARD, PropConstant.DOUBLE_CARD, PropConstant.WHITE_CARD, PropConstant.ACCELERATOR_CARD) : Arrays.asList(PropConstant.GOLDEN_EGG, PropConstant.SILVER_EGG));
        return ResultVo.success(props);
    }

    /**
     * 获取鸡舍
     */
    @GetMapping("/henHouseList")
    public ResultVo getHenHouseList(@RequestHeader(value = "token", required = false, defaultValue = "") String token) {
        Long userId = null;
        if (!ObjectUtils.isEmpty(token)) {
            userId = JwtUtil.verifyToken(token).get("userId", Long.class);
        }
        List<GuProp> props = propService.getHenHouseList(userId);
        return ResultVo.success(props);
    }

    @GetMapping("/acceleratorCard")
    public ResultVo getListByPropId(@RequestHeader(value = "token", required = false, defaultValue = "") String token) {
        Long userId = null;
        if (!ObjectUtils.isEmpty(token)) {
            userId = JwtUtil.verifyToken(token).get("userId", Long.class);
        }
        List<GuProp> props = propService.acceleratorCard(userId);

        return ResultVo.success(props);
    }

    @GetMapping("/back/list")
    public ResultVo getList() {
        List<GuProp> props = propService.list();
        return ResultVo.success(props);
    }

    @GetMapping("/back/detail/{propId}")
    public ResultVo backDetail(@PathVariable int propId) {
        return ResultVo.success(propService.getById(propId));
    }

    /**
     * 修改
     */
    @PostMapping("/back/update")
    public ResultVo update(@RequestBody GuProp guProp) {
        propService.updateById(guProp);
        return ResultVo.success("修改成功");
    }

}
